package cn.hello2018.oa.controller;

import cn.hello2018.oa.biz.GlobalBiz;
import cn.hello2018.oa.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @author jeymingwu
 * @date 2019/2/18 16:58
 */
@Controller
public class GlobalController {

    @Autowired
    private GlobalBiz globalBiz;

    // 打开登陆界面
    @RequestMapping(value = "/to_login")
    public String toLogin() {
        return "login";
    }

    // 验证登录信息
    @RequestMapping(value = "/login")
    public String login(HttpSession session, @RequestParam String sn, @RequestParam String password) {
        Employee employee = globalBiz.login(sn, password);
        if (employee != null) {
            session.setAttribute("employee", employee);
            return "redirect:self";
        } else {
            return "redirect:to_login";
        }
    }

    // 查看个人信息
    @RequestMapping(value = "/self")
    public String self() {
        return "self";
    }

    @RequestMapping(value = "/quit")
    public String quit(HttpSession session) {
        session.setAttribute("employee", null);
        return "redirect:to_login";
    }

    @RequestMapping(value = "/to_change_password")
    public String toChangePassword() {
        return "to_change_password";
    }

    @RequestMapping(value = "change_password")
    public String changePassword(HttpSession session, @RequestParam String old, @RequestParam String new1,
                                 @RequestParam String new2) {
        Employee employee = (Employee) session.getAttribute("employee");
        if (employee.getPassword().equals(old) == false) {
            return "redirect:to_change_password";
        }
        if (new1.equals(new2) == false) {
            return "redirect:to_change_password";
        }
        employee.setPassword(new1);
        globalBiz.changePassword(employee);
        return "self";
    }

}
